<?php

namespace App\Logic\Admin;

use App\Constants\ErrorCode;

class MenusLogic
{
    /**
     * Notes:获取菜单列表
     * 2025/7/10
     * @param array $admin 管理员
     * @return array
     */
    public function menus(array $admin):array
    {
        $adminInfo = \App\Model\Administrator::query()->where('id',$admin['id'])->first();
        return $adminInfo->menus();
    }

    /**
     * Notes:获取菜单列表
     * 2025/7/15
     * @return array
     */
    public function tree()
    {
        $menus = \App\Model\Menu::query()->orderBy('sort')->get();
        return trees($menus->toArray());
    }

    /**
     * Note:获取某一个菜单的详情
     * User: Kong
     * Date: 2025/9/25
     * Time: 14:30
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public function info($data)
    {
        $id = $data['id']??0;
        if ($id<=0){
            throw new \Exception("请传入正确的菜单ID",ErrorCode::PARAM_ERROR);
        }
        $menu = \App\Model\Menu::query()->find($id);
        if (!$menu) {
            throw new \Exception(ErrorCode::getMessage(ErrorCode::DATA_NOT_EXIST),ErrorCode::DATA_NOT_EXIST);
        }
        return $menu;
    }

    /**
     * Note:获取菜单以及子菜单
     * User: Kong
     * Date: 2025/9/25
     * Time: 14:32
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public function menuAndChildren($data)
    {
        try {
            $id = $data['id']??0;
            if ($id<=0){
                throw new \Exception("请传入正确的菜单ID",ErrorCode::PARAM_ERROR);
            }
            $menu = \App\Model\Menu::query()->find($id);
            if (!$menu) {
                throw new \Exception(ErrorCode::getMessage(ErrorCode::DATA_NOT_EXIST),ErrorCode::DATA_NOT_EXIST);
            }
            $children = \App\Model\Menu::query()->where('pid', $id)->get();
            // todo
            return $menu;
        } catch (\Throwable $exception) {
            throw new \Exception($exception);
        }
    }

    /**
     * Note:创建菜单
     * User: Kong
     * Date: 2025/9/25
     * Time: 14:34
     * @param $data
     * @return \App\Model\Menu|\Hyperf\Database\Model\Model
     * @throws \Exception
     */
    public function create($data)
    {
        try {
            // $data['name'] = ucfirst($data['name']);
            $data['path'] = strtolower($data['path']);
            if ($data['pid'] != 0) {
                if (!\App\Model\Menu::query()->where('id', $data['pid'])->exists()) {
                    throw new \Exception('未找到父级菜单', ErrorCode::DATA_NOT_EXIST);
                }
            }
            $menu = \App\Model\Menu::create($data);
            return $menu;
        } catch (\Throwable $exception) {
            throw new \Exception($exception);
        }
    }

    /**
     * Note:更新菜单
     * User: Kong
     * Date: 2025/9/25
     * Time: 14:36
     * @param $data
     * @return array
     * @throws \Exception
     */
    public function update($data)
    {
        try {
            $id = $data['id']??0;
            if ($id<=0){
                throw new \Exception("请传入正确的菜单ID",ErrorCode::PARAM_ERROR);
            }
            $menu = \App\Model\Menu::query()->find($data['id']);
            if (! $menu) {
                throw new \Exception(ErrorCode::getMessage(ErrorCode::DATA_NOT_EXIST), ErrorCode::DATA_NOT_EXIST);
            }

            if ($data['pid'] != 0) {
                if (!\App\Model\Menu::query()->where('id', $data['pid'])->exists()) {
                    throw new \Exception('未找到父级菜单', ErrorCode::DATA_NOT_EXIST);
                }

                if ($data['id'] == $data['pid']) {
                    throw new \Exception('pid 和 id 不能相同',ErrorCode::PARAM_ERROR);
                }
            }

            if ($id != $data['id']) {
                throw new \Exception('数据错误',ErrorCode::PARAM_ERROR);
            }

            $menu->fill($data)->save();
            // 更新成功后返回菜单树
            return [
                'info' => $menu,
                'tree' => trees(\App\Model\Menu::query()->orderBy('sort')->get()->toArray()),
            ];
        } catch (\Throwable $exception) {
            throw new \Exception($exception);
        }
    }

    /**
     * Note:删除菜单
     * User: Kong
     * Date: 2025/9/25
     * Time: 14:40
     * @param $data
     * @return array|bool|int|mixed|null
     * @throws \Exception
     */
    public function destroy($data)
    {
        try {
            $id = $data['id']??0;
            $result = \App\Service\Admin\MenuService::destroyMenu($id);
            return $result;
        } catch (\Throwable $exception) {
            throw new \Exception($exception);
        }
    }
}